import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/pages/promotion/promotion_withdraw/promotion_withdraw_logic.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/extensions/num_ext.dart';
import 'package:flutter_09_game/utils/sound/sound_manager.dart';
import 'package:flutter_09_game/utils/utils_other.dart';
import 'package:flutter_09_game/widgets/btn_dropdown.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class WidgetUserWallet extends StatelessWidget {
  const WidgetUserWallet({super.key, this.playSound = false});

  final bool playSound;

  // SoundManager().playButtonSound();
  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: AppColor.textPrimary.withOpacity(0.05),
        borderRadius: BorderRadius.circular(8.w),
        border:
            Border.all(color: AppColor.textPrimary.withOpacity(0.15), width: 1),
      ),
      child: Row(
        children: [
          Container(
              padding: EdgeInsets.symmetric(horizontal: 8.w),
              child: Obx(() {
                Widget child = Row(
                  children: [
                    AutoImage(
                      UtilsOther().getAmountTagIcon(
                          AppUserManager().currentWalletRx.value.symbol),
                      width: 24.w,
                    ),
                    4.horizontalSpace,
                    Obx(() {
                      return Text(
                        AppUserManager()
                                .currentWalletRx
                                .value
                                .balance
                                ?.formatWallet() ??
                            '',
                        style: TextStyle(
                            fontWeight: FontWeight.w500,
                            fontSize: 14.sp,
                            color: AppColor.textPrimary.withOpacity(0.94)),
                      );
                    }),
                    4.horizontalSpace,
                    AutoImage(
                      Assets.commonDownArrow,
                      width: 16.w,
                    ),
                  ],
                );
                return (AppUserManager().currentWalletRx.value.symbol ?? '')
                        .isEmpty
                    ? child
                    : _buildDropdown(child);
              })),
          GestureDetector(
            onTap: () {
              AppUserManager().loadWalletData(showLoad: true);
            },
            child: Container(
              width: 40.w,
              height: 40.w,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.only(
                    topRight: Radius.circular(8.w),
                    bottomRight: Radius.circular(8.w),
                  ),
                  color: AppColor.navBgColor,
                  border: Border(
                      left: BorderSide(
                          color: AppColor.color465485.withOpacity(0.2),
                          width: 1))),
              alignment: Alignment.center,
              child: AutoImage(
                Assets.commonRefresh,
                width: 20.w,
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildDropdown(Widget child) {
    return BtnDropdown(
        items: AppUserManager()
            .walletList
            .map((item) => item.symbol ?? '')
            .toList(),
        // customHomeStyle: [
        // ModelHomeDropdown()
        // ],
        customHomeStyle: AppUserManager()
            .walletList
            .map((item) => ModelHomeDropdown(
                  symbol: item.symbol,
                  balance: item.balance,
                ))
            .toList(),
        value: AppUserManager().currentWalletRx.value.symbol ?? "",
        onChanged: (value) {
          AppUserManager().selectUserWallet(value);
          if (playSound) {
            SoundManager().playButtonSound();
          }
        },
        child: child);
  }
}
